package com.song.provider.service.impl;

import com.song.provider.dao.BookDao;
import com.song.provider.pojo.Book;
import com.song.provider.service.BookService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2024/1/10
 * @Version 1.0
 **/
@Service
//@Transactional
public class BookServiceImpl implements BookService {

    @Resource
    private BookDao bookDao;

    @Override
    public List<Book> books() {
        return bookDao.findAll();
    }

    @Override
    public Book getBookById(Long id) {
        return bookDao.findById(id).get();
    }

    @Override
    public Book addBook(Book book) {
        return this.bookDao.save(book);
    }

    @Override
    public boolean modifyBook(Book book) {
        return this.bookDao.save(book) != null;
    }

    @Override
    public boolean deleteBookById(Long id) {
        this.bookDao.deleteById(id);
        return  true;
    }

    @Override
    public Boolean stock(Long id, Long count) {
        //根据id查询图书
        Book book = this.bookDao.findById(id).get();
        //修改库存数量
        //判断库存是否充足
        if(book.getAmount()==0 || book.getAmount() < count){
            return false;
        }
        //修改库存数量
        book.setAmount(book.getAmount()-count);
        this.bookDao.save(book);
        return true;
    }
}
